package cookinmaddy.web;


import cookinmaddy.database.DBManager;
import cookinmaddy.model.Ingredient;
import cookinmaddy.model.Recipe;
import cookinmaddy.model.Unit;
import cookinmaddy.templo.TemploManager;
import cookinmaddy.model.UnitManager;
import cookinmaddy.model.IngredientManager;
import cookinmaddy.model.RecipeManager;
import haxe.Unserializer;
import php.Lib;

class CookinMaddyApp
{
	var root : Unit;
	var unitManager : UnitManager;
	var ingredientManager : IngredientManager;
	var recipeManager : RecipeManager;

	public static var _app : CookinMaddyApp;
	private static var TEMPLATE : String = "cookinmaddy.mtt";
	private static var TOP_MENU_TEMPLATE : String = "topmenu.mtt";
	
	public function new(){}
	
	public static function initialize() : CookinMaddyApp
	{
		trace("BEGIN");
		try
		{
			_app = new CookinMaddyApp();
		}
		catch(e : String)
		{
			trace(e);
		}
		trace("END");
		return _app;
	}
	
	public static function home()
	{
		trace("BEGIN");
		try
		{
			var temploader = new templo.Loader(TEMPLATE);
			var r = temploader.execute( { topmenudiv : "", maindiv : "" } );		
			Lib.print(r);
		}
		catch (e :String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}


	public static function listrecipes()
	{
		trace("BEGIN");
		try
		{
			_app.recipeManager = new RecipeManager();
			
			trace("call the unitview ");
			var display : String = _app.recipeManager.getList();
			trace("show the result ");
			php.Lib.print(display);		
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}

	
	public static function listunits()
	{
		trace("BEGIN");
		try
		{
			_app.unitManager = new UnitManager();
			
			trace("call the unitview ");
			var display : String = _app.unitManager.getList();
			trace("give the unitview to templo ");
			trace("show the result ");
			php.Lib.print(display);		
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}
	
	public static function updateunit(data : Dynamic)
	{
		trace("BEGIN");
		try
		{
		
			var unit : Unit = Unserializer.run(data);
			var ret : String = _app.unitManager.update(unit);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}
	
	public static function addrecipe(data : Dynamic)
	{
		trace("BEGIN");
		try
		{
			_app.recipeManager = new RecipeManager();
			trace("serialized:" + data);
			var recipe : Recipe = Unserializer.run(data);
			var ret : String = _app.recipeManager.add(recipe);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}

	public static function addunit(data : Dynamic)
	{
		trace("BEGIN");
		try
		{
			var unit : Unit = Unserializer.run(data);
			var ret : String = _app.unitManager.add(unit);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}
	
	public static function delunit(data : Dynamic)
	{
		trace("BEGIN");
		try
		{
			var unit : Unit = Unserializer.run(data);
			var ret : String = _app.unitManager.delete(unit);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}	

	public static function listingredients()
	{
		trace("BEGIN");
		try
		{
			_app.ingredientManager = new IngredientManager();
			
			trace("call the unitview ");
			var display : String = _app.ingredientManager.getList();
			trace("give the unitview to templo ");
			trace("show the result ");
			php.Lib.print(display);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}
	
	public static function updateingredient(data : Dynamic)
	{
		trace("BEGIN");
		try
		{		
			var ingredient : Ingredient = Unserializer.run(data);
			var ret : String = _app.ingredientManager.update(ingredient);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}
	
	public static function addingredient(data : Dynamic)
	{
		trace("BEGIN");
		try
		{
			var ingredient : Ingredient = Unserializer.run(data);
			var ret : String = _app.ingredientManager.add(ingredient);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}
	
	public static function delingredient(data : Dynamic)
	{
		trace("BEGIN");
		try
		{
			var ingredient : Ingredient = Unserializer.run(data);
			var ret : String = _app.ingredientManager.delete(ingredient);
			trace(ret);
			Lib.print(ret);
		}
		catch (e : String)
		{
			trace("ERROR" + e);
		}
		trace("END");
	}	
}